package light.head.dao.sys;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.FileInfo;
import light.head.model.OrganSynopsis;

import org.springframework.stereotype.Service;
/**
 *	企业简介
 */
@Service
public class OrganSynopsisDAO extends BaseDAO<OrganSynopsis, Serializable>{
	private static final String sql_findAnnex="FROM OrganSynopsis osa WHERE osa.oc = ?";
	
	
	public OrganSynopsis getByOc(Integer code) {
		return (OrganSynopsis) get(sql_findAnnex, code);
	}
	
	public List<FileInfo> findFileByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM FileInfo f WHERE f.code IN (" + codes + ")";
		List<FileInfo> list = find(sql);
		return list;
	}
	
	public RESULT deleteAnnexNotInCodes(Integer code, List<Object> codes) {
		StringBuffer sql = new StringBuffer();
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		for (Object o : codes) {
			sql.append("?,");
			parm.add(o);
		}
		if (sql.length() > 0) {
			sql.insert(0, " AND osa.code NOT IN(").deleteCharAt(sql.length() - 1).append(")");
		}
		sql.insert(0, "DELETE FROM OrganSynopsisAnnex osa WHERE osa.odc = ?");
		execute(sql.toString(), parm.toArray());
		return RESULT.SUCCESS;
	}
	
	
	public RESULT update(OrganSynopsis os) {
		if (null == os) return RESULT.PARAMETER_ERROR;
		
		OrganSynopsis dbos = getByOc(os.getOc());
		if (null == dbos) {
			return RESULT.NOT_FIND;
		}
		
		boolean ok = false;
		
		if ((isNotNull(os.getContent()) && !dbos.getContent().equals(os.getContent())) || null == dbos.getContent() || Sys.NONE.equals(dbos.getContent())) {
			dbos.setContent(os.getContent());
			ok = true;
		}
		
		if (ok) {
			return upd(dbos);
		}
		
		return RESULT.SUCCESS;
		
	}
}
